package cn.iocoder.yudao.module.agent.service.withdrawaccount;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.withdrawaccount.AgentWithdrawAccountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.agent.convert.withdrawaccount.AgentWithdrawAccountConvert;
import cn.iocoder.yudao.module.agent.dal.mysql.withdrawaccount.AgentWithdrawAccountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.agent.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 代理人佣金提现账户 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class AgentWithdrawAccountServiceImpl implements AgentWithdrawAccountService {

    @Resource
    private AgentWithdrawAccountMapper withdrawAccountMapper;

    @Override
    public Long createWithdrawAccount(AgentWithdrawAccountCreateReqVO createReqVO) {
        // 插入
        AgentWithdrawAccountDO withdrawAccount = AgentWithdrawAccountConvert.INSTANCE.convert(createReqVO);
        withdrawAccountMapper.insert(withdrawAccount);
        // 返回
        return withdrawAccount.getId();
    }

    @Override
    public void updateWithdrawAccount(AgentWithdrawAccountUpdateReqVO updateReqVO) {
        // 校验存在
        validateWithdrawAccountExists(updateReqVO.getId());
        // 更新
        AgentWithdrawAccountDO updateObj = AgentWithdrawAccountConvert.INSTANCE.convert(updateReqVO);
        withdrawAccountMapper.updateById(updateObj);
    }

    @Override
    public void deleteWithdrawAccount(Long id) {
        // 校验存在
        validateWithdrawAccountExists(id);
        // 删除
        withdrawAccountMapper.deleteById(id);
    }

    private void validateWithdrawAccountExists(Long id) {
        if (withdrawAccountMapper.selectById(id) == null) {
            throw exception(WITHDRAW_ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public AgentWithdrawAccountDO getWithdrawAccount(Long id) {
        return withdrawAccountMapper.selectById(id);
    }

    @Override
    public AgentWithdrawAccountDO getWithdrawAccountByUserId(Long userId){
        return withdrawAccountMapper.selectOne(AgentWithdrawAccountDO::getUserId, userId);
    }

    @Override
    public List<AgentWithdrawAccountDO> getWithdrawAccountList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return withdrawAccountMapper.selectBatchIds(ids);
    }

    @Override
    public List<AgentWithdrawAccountDO> getWithdrawAccountListByUserIds(Collection<Long> userids){
        if(CollUtil.isEmpty(userids)){
            return ListUtil.empty();
        }

        return withdrawAccountMapper.selectList(AgentWithdrawAccountDO::getUserId, userids);
    }

    @Override
    public PageResult<AgentWithdrawAccountDO> getWithdrawAccountPage(AgentWithdrawAccountPageReqVO pageReqVO) {
        return withdrawAccountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AgentWithdrawAccountDO> getWithdrawAccountList(AgentWithdrawAccountExportReqVO exportReqVO) {
        return withdrawAccountMapper.selectList(exportReqVO);
    }

}
